/*  This file is part of -_-.

    -_- is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    -_- is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with -_-.  If not, see <http://www.gnu.org/licenses/>.

    Copyright 2012-2013 Oliver Katz */

#include "mod_bus.h"

namespace sleepyface
{
    namespace arch
    {
        namespace modules
        {
            Bus::Bus() : ArchModule()
            {
                setName("bus");
            }

            int Bus::signal(ModuleSignal s, Architecture &a)
            {
                if (s.getCode() == signalInitialize)
                {
                    ip = s.getData(0).getString();
                    port = s.getData(1).getInt();
                    client = z_ipc_client_init_global((char *)ip.c_str(), port);
                    return 0;
                }
                else if (s.getCode() == signalUninitialize)
                {
                    z_ipc_client_release(client);
                    return 0;
                }
                else if (s.getCode() == signalUpdate)
                {
                    if (z_ipc_client_connect(&client) != 0)
                        return 1;
                    stringstream ss;
                    ss << a.getName() << "@" << ip << ":" << port;
                    z_data req = z_data_wrap((char *)ss.str().c_str(), Z_TYPEID_STR, ss.str().size()*sizeof(char));
                    z_ipc_client_send_request(&client, &req);
                    z_data res = z_ipc_client_recieve_response(&client);
                    if (z_data_src_get(&res) == NULL)
                        return 2;
                    char *save;
                    for (char *p = strtok_r((char *)res.src, "\n", &save); p != NULL; p = strtok_r(NULL, "\n", &save))
                    {
                        char *port = strchr(p, ':')+1;
                        *(port-1) = 0;
                        char *ip = strchr(p, '@')+1;
                        *(ip-1) = 0;
                        pair<string, int> tmp;
                        tmp.first = string(ip);
                        tmp.second = atoi(port);
                        apps[string(p)] = tmp;
                    }
                    z_ipc_client_disconnect(&client);
                    return 0;
                }
                else
                {
                    return 0;
                }
            }

            ModuleValue Bus::get(string k0, string k1, Architecture &a)
            {
                if (apps.find(k0) == apps.end())
                    return ModuleValue();

                if (k1.compare("ip") == 0)
                    return ModuleValue(ModuleValue::_string, apps[k0].first);
                else if (k1.compare("port") == 0)
                    return ModuleValue(ModuleValue::_int, apps[k0].second);
                else
                    return ModuleValue();
            }

            vector<string> Bus::list(Architecture &a)
            {
                vector<string> rtn;
                for (map<string, pair<string, int> >::iterator i = apps.begin(); i != apps.end(); i++)
                    rtn.push_back(i->first);
                return rtn;
            }
        }
    }
}
